package com.game.service.beans.livepk;

import com.game.service.app.get_score.JsonBean;
import com.game.service.beans.GameMessage;
import com.game.service.beans.Login;
import com.game.service.beans.game.*;
import com.game.service.robot.IGameMessage;
import com.game.service.socket.SocketServer;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.java_websocket.WebSocket;

import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import static com.game.service.define.GameDefine.*;

public class LivePKMgr implements IGameMessage {
    SocketServer mKernel;
    /**
     * 游客
     */
    private HashMap<Integer, Login> userMap = new HashMap<>();

    public HashMap<Integer, Login> getUserMap() {
        return userMap;
    }

    /**
     * 游客websocket维护列表
     */
    private HashMap<Integer, WebSocket> webMap = new HashMap<>();

    public HashMap<Integer, WebSocket> getWebMap() {
        return webMap;
    }

    private HashMap<String, MatchImageHead> imageHeadMap = new HashMap<>();

    public HashMap<String, MatchImageHead> getImageHeadMap() {
        return imageHeadMap;
    }

    public LivePKMgr() {
        mKernel = SocketServer.getKernel();
    }

    public void addLivePKMatch() {
        CreateLivePK livePK = new CreateLivePK();
        livePK.setUidA(10159);
        livePK.setUidB(10150);
        livePK.setMatchWinNumber(1);
        mKernel.createLivePKTable(livePK);
    }


    @Override
    public void send(int main, int sub, Object obj) {
        //  System.out.printf("LivePKMgr:%s-%s", main, sub);
        switch (sub) {
            case GAME_C_SUB_MATCH_WEAPON_RETURN:
                // System.out.println("LivePKMgr:GAME_C_SUB_MATCH_WEAPON_RETURN");
                // sendToSpectatorWeapon((TableInfo) obj);
                break;

            case GAME_C_SUB_MATCH_UP_RETURN://游戏房间信息
                //  System.out.println("LivePKMgr:GAME_C_SUB_MATCH_UP_RETURN");
                TableInfo table = (TableInfo) obj;
                sendToSpectator(table);

                break;
            case GAME_C_SUB_MATCH_HIT_RETURN://击中返回
                // System.out.println("LivePKMgr:GAME_C_SUB_MATCH_HIT_RETURN");
                TableInfo tableMatchHit = (TableInfo) obj;
                sendToSpectatorMatchHit(tableMatchHit);
                break;


            case GAME_C_SUB_MATCH_CREATE_TABLE_RETURN://创建房间成功
                //  System.out.println("LivePKMgr:GAME_C_SUB_MATCH_CREATE_TABLE_RETURN");
                TableInfo tableCreate = (TableInfo) obj;

                break;
            case GAME_C_SUB_MATCH_OVER_RETURN:
                //  System.out.println("LivePKMgr:GAME_C_SUB_MATCH_OVER_RETURN");
                TableInfo tableOver = (TableInfo) obj;
                sendToSpectatorOver(tableOver);
//
                break;

            case GAME_C_SUB_MATCH_AGAIN_RETURN: //收到对方玩家准备决战到底
                // System.out.println("LivePKMgr:GAME_C_SUB_MATCH_AGAIN_RETURN");


                break;

            default:
                break;
        }
    }


    public void sendToSpectator(TableInfo tableInfo) {
        userMap.forEach((key, login) -> {
            if (Objects.equals(login.getViewingTableId(), tableInfo.getTableId())) {
                //System.out.println("LivePKMgr" + tableInfo);
                mKernel.sendFF(webMap.get(login.getUid()), GAME_MAIN, GAME_C_SUB_MATCH_UP_RETURN, tableInfo);
            }
        });

    }

    public void sendToSpectatorWeapon(TableInfo tableInfo) {
        userMap.forEach((key, login) -> {
            if (Objects.equals(login.getViewingTableId(), tableInfo.getTableId())) {
                mKernel.sendFF(webMap.get(login.getUid()), GAME_MAIN, GAME_C_SUB_MATCH_WEAPON_RETURN, tableInfo);
            }
        });

    }

    public void sendToSpectatorOver(TableInfo tableInfo) {
        userMap.forEach((key, login) -> {
            if (Objects.equals(login.getViewingTableId(), tableInfo.getTableId())) {
                mKernel.sendFF(webMap.get(login.getUid()), GAME_MAIN, GAME_C_SUB_MATCH_OVER_RETURN, tableInfo);
            }
        });

    }

    public void sendToSpectatorMatchHit(TableInfo tableInfo) {
        userMap.forEach((key, login) -> {
            if (Objects.equals(login.getViewingTableId(), tableInfo.getTableId())) {
                mKernel.sendFF(webMap.get(login.getUid()), GAME_MAIN, GAME_C_SUB_MATCH_HIT_RETURN, tableInfo);
            }
        });

    }

    public void sendToSpectatorMatchKillOnce(KillOncePosition killOncePosition) {
        userMap.forEach((key, login) -> {
            if (Objects.equals(login.getViewingTableId(), killOncePosition.getTableId())) {
                mKernel.sendFF(webMap.get(login.getUid()), GAME_MAIN, GAME_C_SUB_MATCH_KILL_ONCE_RETURN, killOncePosition);
            }
        });

    }

    /***
     *   发送比赛双方的头像给围观者
     * @param mTable 本桌信息
     */
    public void sendMatchPlayerImageHead(int requestId, TableInfo mTable) {

        MatchImageHead imageHead = getImageHeadMap().get(mTable.getTableId());
        if (imageHead != null) {
            System.out.println("发送缓存头像信息");
            mKernel.send(getWebMap().get(requestId), GAME_MAIN, GAME_C_SUB_MATCH_HEAD_INFO_RETURN, imageHead);
        }
    }

    public void loginSpectator(WebSocket conn, String message) {
        Type type = new TypeToken<GameMessage<Login>>() {
        }.getType();//无敌好用
        GameMessage<Login> data = new Gson().fromJson(message, type);
        Login login = data.getCmd();
        getUserMap().put(login.getUid(), login);
        getWebMap().put(login.getUid(), conn);
        mKernel.send(conn, GAME_MAIN, GAME_C_SUB_SPECTATOR_ENTER_RETURN, "游客登录成功");
    }

    public void viewingLivePKMatch(String json) {
        Type type = new TypeToken<GameMessage<MatchUp>>() {
        }.getType();//无敌好用

        GameMessage<MatchUp> data = new Gson().fromJson(json, type);
        MatchUp matchUp = data.getCmd();

        TableInfo mTable = mKernel.getTableMap().get(matchUp.getTableId());
        JsonBean<String> jsonBean = new JsonBean<>();
        if (mTable == null) {
            jsonBean.code = 1;
            jsonBean.msg = "围观的比赛房间已经解散...";
        } else {
            jsonBean.code = 0;
            jsonBean.msg = "欢迎围观...";
        }
        System.out.println("LivePKMgr" + jsonBean.msg);
        mKernel.sendFF(getWebMap().get(matchUp.getUid()), GAME_MAIN, GAME_C_SUB_VIEWING_LIVE_PK_MATCH_RETURN, jsonBean);
    }

    public void removeSpectator(int uid) {
        getWebMap().remove(uid);
        getUserMap().remove(uid);
    }

    public void updateSpectators(TableInfo mTable) {
        if (mTable.getLivePK() == null) {
            //非主播类PK房间,不做统计
            return;
        }
        AtomicInteger mSpectators= new AtomicInteger();
        getUserMap().forEach((key, userInfo) -> {
            if (Objects.equals(userInfo.getViewingTableId(), mTable.getTableId())) {
                mSpectators.addAndGet(1);
            }
        });
        mTable.getLivePK().setSpectators(mSpectators.get());
    }
}
